package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConexionDAO {
	
	protected Connection conexion = null;
	protected Statement estamento = null;
	protected PreparedStatement preparada = null;
	protected ResultSet resultado = null;
	protected static final String DATASOURCE_NAME = "java:comp/env/jdbc/proyectopablowebsockets";
	
	protected void getConexion(){
		if(conexion==null){
			try{
				Context contextoInicial = new InitialContext();
				DataSource ds = (DataSource)contextoInicial.lookup("jdbc/proyectopablowebsockets");
				conexion = ds.getConnection();
			} catch(NamingException e){
				e.printStackTrace();
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
	protected void cerrar(){
		
		if(resultado != null){
			try {
				resultado.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			resultado = null;
		}
		if(estamento != null){
			try {
				estamento.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			estamento = null;
		}
		if(preparada != null){
			try {
				preparada.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			preparada = null;
		}
		if(conexion != null){
			try {
				conexion.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
			conexion = null;
		}
	}

}
